package org.gl.rtsp;

import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

public class URL {
	private Map<String, String> paramMap = new HashMap<String, String>();
	String url;

	public URL(String uRLString) {
		url = uRLString;
	}

	public void analysis() {
		paramMap.clear();
		if (!"".equals(url)) {// 如果URL不是空字符串
			try {
				URIParse parse = new URIParse(url);
				protocol = parse.getProtocolName();
				host = parse.getHost();
				port = ""+parse.getPort();
				uri = parse.getUri();
				paramMap.putAll(parse.getParamMap());
			} catch (URISyntaxException e) {
				e.printStackTrace();
			}
			
//			protocol = url.substring(0, url.indexOf("://"));
//			System.out.println("protocol:"+protocol);
//			String[] ss = url.split("/");
//			String hostport = ss[2];
//			host = hostport.substring(0, hostport.indexOf(":"));
//			System.out.println("host:"+host);
//			port = hostport.substring(hostport.indexOf(":") + 1,
//					hostport.length());
//			System.out.println("port:"+port);
//			uri = url
//					.substring(url.indexOf(port) + port.length(), url.length());
//			System.out.println("uri:"+uri);
//			if (uri.indexOf("?")<0) {
//				return;
//			}
//			String parameterString = uri.substring(uri.indexOf("?")+1,uri.length());
//			System.out.println("parameterString:"+parameterString);
//			String paramaters[] = parameterString.split("&");
//			for (String param : paramaters) {
//				String values[] = param.split("=");
//				paramMap.put(values[0], values[1]);
//				System.out.println("parameater:"+values[0]+":"+values[1]);
//			}
		}
	}

	public String host;
	public String port;
	public String uri;
	public String protocol;
	public int getPort() {
		return Integer.parseInt(port);
	}
	public String getParam(String name) {
		return paramMap.get(name);
	}

	public static void main(String[] args) {
		String URLString = "rtsp://192.168.4.45:" + 5540
				+ "/proxy?url=rtsp://admin:123@192.168.32.45:3330/dfas/&fasdf=fas";
		URL url = new URL(URLString);
		url.analysis();
		System.out.println(url.toString());
		System.out.println(3*0.1);
	}
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append(protocol);
		sb.append("://");
		sb.append(host);
		sb.append(":");
		sb.append(port);
		if (uri!=null&&uri.length()>0) {
			sb.append(uri);
		}
		return sb.toString();
	}
}
